package com.example.stlink.activitys;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.stlink.R;
import com.example.stlink.model.constants.ModelFieldConstants;
import com.example.stlink.utils.CommonUtils;
import com.example.stlink.utils.activityUtil.LoginUtil;

/**
 * 学生端登录
 */
public class LoginActivity extends AppCompatActivity {


    private EditText etPwd;
    private EditText etAccount;
    private CheckBox cbRememberPwd;
    private Button btLogin;
    private TextView lgRegister;
    private TextView tvLgTitle;
    private ImageView lgUserIcon;

    private Integer roleId;
    private Bundle bundle;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();

    }

    /**
     * 初始化当前页面
     */
    private void initView(){
        //修改状态栏字体颜色
        CommonUtils.changeStatusBar(this);

        etPwd = findViewById(R.id.pwd_lg);
        etAccount = findViewById(R.id.id_lg);
        cbRememberPwd = findViewById(R.id.lg_rememberPsd);
        btLogin = findViewById(R.id.bt_login);
        lgRegister = findViewById(R.id.lg_register);
        tvLgTitle = findViewById(R.id.tv_lg_title);
        lgUserIcon = findViewById(R.id.lg_userIcon);

        bundle = getIntent().getExtras();
        roleId = (Integer) bundle.get(ModelFieldConstants.ROLE_ID);

        if(roleId == 1){
            tvLgTitle.setText("教师端登录");
            lgUserIcon.setImageResource(R.mipmap.teacher);
        }

        //加载用户id，密码到页面
        initLogin(etAccount, etPwd, cbRememberPwd);
        //为当前页面按钮添加点击监听事件
        setOnclickListener();
    }

    /**
     * 添加按钮点击监听事件
     */
    @SuppressLint("ResourceAsColor")
    private void setOnclickListener(){

        LoginUtil.userLogin(LoginActivity.this, btLogin, etAccount, etPwd, cbRememberPwd );

        //用户注册
        lgRegister.setOnClickListener(v -> {
            lgRegister.setTextColor(R.color.crimson);
            lgRegister.getPaint().setUnderlineText(true);
            Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
        });
    }

    /**
     * 初始化登录界面
     * 读取本地账户密码信息，并加载到界面中
     * @param etAccount 账户昵称输入
     * @param etPwd 密码输入
     * @param cbRememberPwd 密码是否保存选择框
     */
    public void initLogin(EditText etAccount, EditText etPwd, CheckBox cbRememberPwd){
        String spFileName = getResources()
                .getString(R.string.shared_preferences_file_name);
        String accountKey = getResources()
                .getString(R.string.login_account_name);
        String passwordKey = getResources()
                .getString(R.string.login_password);
        String rememberPasswordKey = getResources()
                .getString(R.string.login_remember_password);

        SharedPreferences spFile = getSharedPreferences(
                spFileName,
                Context.MODE_PRIVATE
        );

        String account = spFile.getString(accountKey, null);
        String password = spFile.getString(passwordKey, null);
        boolean rememberPassword = spFile.getBoolean(rememberPasswordKey, false);
        if(account != null && !TextUtils.isEmpty(account)){
            etAccount.setText(account);
            etAccount.setSelection(account.length());
        }
        if (password != null && !TextUtils.isEmpty(password)) {
            etPwd.setText(CommonUtils.encryptAndDencrypt(password));
        }
        cbRememberPwd.setChecked(rememberPassword);
    }

}
